{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data curation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import some necessary functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, pydicom, sys\n",
    "import numpy as np\n",
    "from Plot_And_Scroll_Images.Plot_Scroll_Images import plot_Image_Scroll_Bar_Image, plot_scroll_Image\n",
    "import SimpleITK as sitk\n",
    "from Image_Array_And_Mask_From_Dicom_RT import Dicom_to_Imagestack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding the Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find where we put our data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_path = r'K:\\Morfeus\\BMAnderson\\CNN\\Data\\Data_Liver\\Liver_Disease_Ablation_Segmentation\\Images'\n",
    "print('We have ' + str(len(os.listdir(data_path))) + ' patients!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ensuring contour fidelity..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Note that we've set 'get_images_mask' to False, this means we won't be getting any of the image data, just looking at the dicom RT files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Dicom_Reader = Dicom_to_Imagestack(get_images_mask=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Dicom_Reader.down_folder(data_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What ROI names do we have?\n",
    "\n",
    "#### This will tell us all the unique roi names, hence all_rois"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "for roi in Dicom_Reader.all_rois:\n",
    "    print(roi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make contour associations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### We have quite a few contour names here.. now, we can either change the ROI names in the RT files, or make an associations file\n",
    "\n",
    "#### The associations file associates a contour name with another one {'Current contour':'Desired name'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "associations = {'Liver_BMA_Program_4':'Liver',\n",
    "                'bma_liver':'Liver',\n",
    "                'best_liver':'Liver',\n",
    "                'tried_liver':'Liver'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tell the Dicom_Reader that we want to set the associations, get the images and mask for contour 'Liver'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Dicom_Reader.set_associations(associations)\n",
    "Dicom_Reader.set_get_images_and_mask(True)\n",
    "Dicom_Reader.set_contour_names(['liver'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Re-write RTs\n",
    "#### This is commented out, because if I run it, then the example above won't show any different contour names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dicom_Reader.associations = associations\n",
    "# for RT in Dicom_Reader.all_RTs:\n",
    "#     Dicom_Reader.rewrite_RT(RT)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pulling images and mask"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### We'll first do this with one patient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "patient_data_path = os.path.join(data_path,os.listdir(data_path)[0],'CT 2')\n",
    "Dicom_Reader.Make_Contour_From_directory(patient_data_path)\n",
    "print('Done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## View images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The images and mask are saved within the Dicom_Reader class, so we just have to load them"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Images = Dicom_Reader.ArrayDicom\n",
    "mask = Dicom_Reader.mask # This is the mask"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Viewing nifti handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dicom_handle = Dicom_Reader.dicom_handle\n",
    "annotations_handle = Dicom_Reader.annotation_handle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Threshold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Images[Images<-200] = -200\n",
    "Images[Images>200] = 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plot_Image_Scroll_Bar_Image(Images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Images[mask==1] += 300"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Turn predictions into an RT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your prediction needs to be of the form [# Images, rows, columns, #classes + 1], the [...,0] is background"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prediction = np.zeros(Images.shape+(2,)) # Two classes, background and test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prediction[...,300:400,300:400,1] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Need to pass the prediction, an output directory, and ROI_Names list equal to the number of classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "help(Dicom_Reader.with_annotations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_path = os.path.join(data_path,os.listdir(data_path)[0],'CT 2','new_RT')\n",
    "ROI_Names=['test']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Dicom_Reader.with_annotations(prediction,out_path=out_path,ROI_Names=ROI_Names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Recap\n",
    "\n",
    "### Checking ROI contour names and making associations\n",
    "\n",
    "### Loading in image and mask from desired contour name\n",
    "\n",
    "### Viewing images and mask\n",
    "\n",
    "### Turning predictions into an RT Structure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Separate into Train/Test/Validation\n",
    "\n",
    "### This is also important, but I would recommend using the 'Parallel' approach available in https://github.com/brianmanderson/Dicom_Data_to_Numpy_Arrays"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
